package org.brain.tool;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;


import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import org.brain.dbo.DBOperate;
import org.brain.pojos.Document;
import org.brain.pojos.User;


@ManagedBean(name="toolBean")
@RequestScoped
public class ToolBean {
	
	public String contextPath;
	
	public static User getUserSession()
	{
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(true);
		return (User) session.getAttribute("user");
	}

	public static String getRequestParameter(String name)
	{
		return (String)FacesContext.getCurrentInstance()
        						.getExternalContext()
        						.getRequestParameterMap().get(name);
	}
	
	public String getContextPath() {
		 return FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
	}
	public void setContextPath(String contextPath) {
		this.contextPath = contextPath;
	}
	public static boolean UpdateImage(String path)
	{
			  System.out.println("path "+path);
			  String[]a = path.split("/");
			  String pathImage = a[a.length-1];
			  System.out.println("path 的值是："+path+" leng"+a.length);
			  File file = null;
			  FileOutputStream fos = null;
			  
			  String outfile = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath()+"/"+pathImage;
			 
			  byte[] Buffer = new byte[4096];
			  try {
				FileInputStream in = new FileInputStream(new File(path));
				
			   file = new File(outfile);
			   if (!file.exists()) {
			    file.createNewFile(); // 如果文件不存在，则创建
			   }
			   fos = new FileOutputStream(file);
			  
			   int size = 0;
			   
			   in.read(Buffer);
			   
			   fos.write(Buffer);
			   fos.close();
			  } catch (Exception e) {
			   System.out.println( e.getMessage());
			  } 
			 
			  System.out.println("文件输出路径"+outfile);
		return false;
	}
	
	public void showImage()
	{
		FileOutputStream fos = null;
		  //InputStream is = null;
		  byte[] Buffer = null;
		  Document doc = DBOperate.getDocument(43);
		  //Buffer = doc.getImage();
		  
		  try{
		File file = new File("d://b.jpg");
		   if (!file.exists()) {
			   
		    file.createNewFile(); // 如果文件不存在，则创建
		   
		   }
		   fos = new FileOutputStream(file);

		   fos.write(Buffer);
		   
		   fos.close();
		   
		  } catch (Exception e) {
		   System.out.println( e.getMessage());
		  }
	}
	public static void main(String[] args) {
		ToolBean tool = new ToolBean();
		tool.showImage();
	}

}
